package scau.online.web.user;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import scau.cty.base.common.BaseController;
import scau.cty.base.common.IBaseService;
import scau.cty.dao.IGoodsMapper;
import scau.cty.dictionary.Code;
import scau.cty.entity.Brand;
import scau.cty.entity.Goods;
import scau.cty.entity.Slide;
import scau.cty.service.IGoodsService;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Sunlight on 2017/3/21.
 * 商品信息增删查改控制器
 */
@Api(value = "商品信息管理控制器",description = "商品信息接口")
@RestController
@RequestMapping(value = "/goods",produces = "application/json")
public class GoodsController extends BaseController{
    @Autowired
    private IBaseService<Goods> goodsIBaseService;
    @Autowired
    private IBaseService<Brand> brandIBaseService;
    @Autowired
    private IBaseService<Slide> slideIBaseService;
    @Autowired
    private IGoodsService iGoodsService;
    /**
     * 商品列表  根据pageSize和pageNow查询商品 order为排序方式
     * @param pageSize
     * @param pageNow
     * @param order
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "商品列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNow",value = "请求页码",required = true,paramType = "query"),
            @ApiImplicitParam(name = "order",value = "排序方式，不传参数为默认排序 1优先级排序，2人气排序，3销量排序，4综合排序，5价格降序排序",
                    required = true,paramType = "query")
    })
    @GetMapping("/goods_list")
    public String getGoodsList(Integer pageSize,Integer pageNow,Integer order) throws Exception{
        Map map = new LinkedHashMap<>();
        if(pageNow == null || pageNow == 0){
            pageNow = 1;
        }
        if(pageSize == null || pageSize ==0){
            sendCode(map, Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        List<Goods> goodsList = new ArrayList<>();
        if(order == null || order == 0){
            //默认排序
            goodsList = goodsIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"id");
//            PageHelper.startPage(pageNow,pageSize); 分页插件
//            List<Goods> goodsList = goodsIBaseService.findAll();
        }else if(order == 1){ //优先级排序
            goodsList = goodsIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"priority");
        }else if(order == 2){ //人气排序
            goodsList = goodsIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"click_count");
        }else if(order == 3){ //销量排序
            goodsList = goodsIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"sale_count");
        }else if(order == 4){ //综合排序
            goodsList = iGoodsService.selectOrderBySaleAndClick(pageNow,pageSize);
        }else if(order == 5){ //价格降序排序
            goodsList = goodsIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"price");
        }
        map.put("goodsList",goodsList);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);

    }

    /**
     * 品牌列表接口
     * @param pageSize 前端参数
     * @param pageNow 前端参数
     * @return
     */
    @ApiOperation(value = "品牌列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNow",value = "请求页码",required = true,paramType = "query")
    })
    @GetMapping("/brand")
    public String getBrandList(Integer pageSize,Integer pageNow){
        Map map = new LinkedHashMap<>();
        if(pageNow == null || pageNow == 0){
            pageNow = 1;
        }
        if(pageSize == null || pageSize ==0){
            sendCode(map, Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        PageHelper.startPage(pageNow,pageSize);
        List<Brand> brandList = brandIBaseService.findAll();
        map.put("brandList",brandList);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

    /**
     * 轮播图列表接口
     * @return
     */
    @ApiOperation(value = "轮播图列表接口")
    @GetMapping("slide")
    public String getSlideList(){
        Map map = new LinkedHashMap();
        List<Slide> slideList = slideIBaseService.findAll();
        map.put("slideList",slideList);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

    /**
     * 模糊搜索玩具接口 标题 种类 品牌 都可以
     * @param condition
     * @param pageSize
     * @param pageNow
     * @return
     */
    @ApiOperation(value = "搜索接口，标题，品牌，种类都可，模糊搜索")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNow",value = "请求页码",required = true,paramType = "query"),
            @ApiImplicitParam(name = "condition",value = "搜素条件",required = true,paramType = "query")
    })
    @GetMapping("search")
    public String getBySearch(String condition,Integer pageSize,Integer pageNow){
        Map map = new LinkedHashMap();
        if(pageNow == null || pageNow == 0){
            pageNow = 1;
        }
        if(pageSize == null || pageSize ==0){
            sendCode(map, Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        if(condition == null || condition ==""){ //搜索条件为空 默认 排序
            PageHelper.startPage(pageNow,pageSize);
            List<Goods> goodsList = goodsIBaseService.findAll();
            map.put("goodsList",goodsList);
            sendCode(map,Code.OPERATION_SUCCESS);
            return JSON.toJSONString(map);
        }else{  //正常搜索条件
            PageHelper.startPage(pageNow,pageSize);
            List<Goods> goodsList = goodsIBaseService.listEntityByCondition(condition,"brand","title","type");
            map.put("goodsList",goodsList);
            sendCode(map,Code.OPERATION_SUCCESS);
        }
        return JSON.toJSONString(map);
    }

    /**
     * 商品详情接口
     * @param goodsId
     * @return
     */
    @ApiOperation(value = "商品详情接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "goodsId",value = "商品唯一标识id",required = true,paramType = "query")
    })
    @GetMapping("/goods_detail")
    public String getGoodsDetail(String goodsId) {
        Map map = new LinkedHashMap();
        if (goodsId == null || goodsId == "") {
            sendCode(map, Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        Goods goods = goodsIBaseService.findByKey(goodsId);
        map.put("goods", goods);
        sendCode(map, Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }
}
